﻿
namespace SvGame.Controller;

public partial class GameHandler
{
    /// <summary>
    /// 用户登录
    /// </summary>
    /// <param name="pack"></param>
    public int C2LUserLogin(MyPackage pack)
    {
        pack.GetMsg(out C2LUserLogin req);
        var rsp = new L2CUserLogin();

        if (req.Account.Length < Conf.ValueInt(EGlobalId.账号长度最小)
            || req.Account.Length > Conf.ValueInt(EGlobalId.账号长度最大))
        {
            return pack.Send(rsp, EProtocolResult.账号长度不符);
        }
        var dtNow = DateTime.Now;
        var user = new User() { 
            Account = req.Account,
        };
        if (DbRedis.ALoad(ref user))
        {
            if (user.Password != req.Password)
            {
                return pack.Send(rsp, EProtocolResult.密码错误);
            }
        }
        else
        {
            user.Uuid = IdMgr.CreateUuid();
            user.Account = req.Account;
            user.Password = req.Password;
            user.TimeRegister = dtNow;
        }
        user.TimeLogin = dtNow;
        user.Token = Util.SGUID();
        rsp.UserId = user.Uuid;
        rsp.Token = user.Token;
        DbRedis.ASave(user);

        return pack.Send(rsp);
    }
}
